﻿using DreamCMS.BLL;
using DreamCMS.BLL.Common;
using DreamCMS.Entity;
using DreamCMS.WebCore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DreamCMS.Web.Admin
{
    public partial class Login : AdminPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) //客户端回传
            {
                UserManager.ClearCache();
                if (Request.Form["login"] != null)
                {
                    //说明是登录
                    string userName = StringHelper.UrlDecode(Request.Form["username"].Trim());
                    string password = Encryption.GetMd5ConfigHash(StringHelper.UrlDecode(Request.Form["password"]), 32);
                    string verifycode = Request.Form["verifycode"].Trim();
                    int expires = Request.Form["keeplog"] == "keeplog" ? 43200 : 0;
                    if (string.IsNullOrEmpty(verifycode) || verifycode != ServerHelper.VerifyCode)
                    {
                        lastMsg.Visible = true;
                        lastMsg.InnerHtml = "<p>验证码错误</p>";
                        return;
                    }
                    UserInfo user = UserManager.GetUser(userName, password);
                    if (user != null)
                    {
                        if (user.Status == 0)
                        {
                            lastMsg.Visible = true;
                            lastMsg.InnerHtml = "<p>用户已停用，请联系管理员。</p>";
                        }
                        else //用户名密码正确
                        {
                            LoginHelper.WriteUserInfoCookie(user.Userid, user.UserName, user.Password, expires);//写入cookie用户信息
                            Response.Redirect("default.aspx");
                        }
                    }
                    else
                    {
                        lastMsg.Visible = true;
                        lastMsg.InnerHtml = "<p>用户名或密码错误</p>";
                    }
                }
            }
            else //直接访问
            {
                //若登陆成功，则跳转到管理页。
                if (this.IsLogin)
                {
                    Response.Redirect("~/admin/default.aspx");
                }
                else
                {
                    if (!string.IsNullOrEmpty(this.IsLoginMessage))
                    {
                        lastMsg.InnerHtml = "<p>" + Request.QueryString["msg"] + "</p>";
                        lastMsg.Visible = true;
                    }
                }
            }
        }
    }
}